#include <QGraphicsDropShadowEffect>
#include "mainwindow.h"
#include "ui_mainwindow.h"

namespace WordMemory
{
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
        , wordPage(new WordPage(this))
        , studyPage(new StudyPage(this))
        , dataPage(new DataPage(this))
        , communityPage(new CommunityPage(this))
        , userPage(new UserPage(this))
        , vocabWordsPage(nullptr)
    {
        ui->setupUi(this);
        setupConnections();
        setupStyles();

        ui->contentStack->addWidget(wordPage);
        ui->contentStack->addWidget(studyPage);
        ui->contentStack->addWidget(dataPage);
        ui->contentStack->addWidget(communityPage);
        ui->contentStack->addWidget(userPage);

        // 默认显示 wordPage
        ui->wordButton->setChecked(true);
        ui->contentStack->setCurrentWidget(wordPage);
    }

    MainWindow::~MainWindow()
    {
        delete ui;
    }

    void MainWindow::setupConnections()
    {
        connect(studyPage, &StudyPage::showVocabularyWordsPage, this, &MainWindow::on_showVocabWordsPage);
        connect(studyPage, &StudyPage::searchWordAndSwitchPage, this, &MainWindow::onSearchWordAndSwitchPage);
    }

    void MainWindow::setupStyles()
    {
        QString buttonStyle = R"(
            QPushButton {
                font-family: "Microsoft YaHei";
                font-weight: bold;
                text-align: left;
                padding-left: 50px;
                padding-right: 16px;
                padding-top: 12px;
                padding-bottom: 12px;
                font-size: 16px;
                background-color: #ffffff;
                color: #595959;
                margin-left: 5px;
                margin-right: 5px;
                margin-top: 5px;
                border-radius: 5px;
                border: none;
            }
            QPushButton::icon {
                margin-right: 8px;
            }
            QPushButton:hover {
                background-color: #e6f7ff;
            }
            QPushButton:checked {
                background-color: #1890ff;
                color: #ffffff;
            };)";

        ui->wordButton->setStyleSheet(buttonStyle);
        ui->studyButton->setStyleSheet(buttonStyle);
        ui->dataButton->setStyleSheet(buttonStyle);
        ui->communityButton->setStyleSheet(buttonStyle);
        ui->userButton->setStyleSheet(buttonStyle);

        // 设置侧栏按钮为可选中
        ui->wordButton->setCheckable(true);
        ui->studyButton->setCheckable(true);
        ui->dataButton->setCheckable(true);
        ui->communityButton->setCheckable(true);
        ui->userButton->setCheckable(true);

        // 为卡片容器添加阴影效果
        QGraphicsDropShadowEffect *cardShadowEffect = new QGraphicsDropShadowEffect(this);
        cardShadowEffect->setBlurRadius(12);
        cardShadowEffect->setXOffset(0);
        cardShadowEffect->setYOffset(4);
        cardShadowEffect->setColor(QColor(0, 0, 0, 0.1 * 255));
        ui->cardWidget->setGraphicsEffect(cardShadowEffect);
        // 为侧栏添加阴影效果
        QGraphicsDropShadowEffect *sidebarShadowEffect = new QGraphicsDropShadowEffect(this);
        sidebarShadowEffect->setBlurRadius(12);
        sidebarShadowEffect->setXOffset(0);
        sidebarShadowEffect->setYOffset(4);
        sidebarShadowEffect->setColor(QColor(0, 0, 0, 0.1 * 255));
        ui->sidebar->setGraphicsEffect(sidebarShadowEffect);
    }

    void MainWindow::on_showVocabWordsPage(const Vocabulary &vocab)
    {
        if (vocabWordsPage) {
            ui->contentStack->removeWidget(vocabWordsPage);
            delete vocabWordsPage;
            vocabWordsPage = nullptr;
        }

        vocabWordsPage = new VocabWordsPage(vocab, this);

        ui->contentStack->addWidget(vocabWordsPage);
        ui->contentStack->setCurrentWidget(vocabWordsPage);

        connect(vocabWordsPage, &VocabWordsPage::backToStudyPage, this, &MainWindow::on_backToStudyPage);
        connect(vocabWordsPage, &VocabWordsPage::wordSelected, this, &MainWindow::on_wordSelected);
    }

    void MainWindow::on_backToStudyPage()
    {
        ui->contentStack->setCurrentWidget(studyPage);
        switchButtonCheckedState(ui->studyButton);
    }

    void MainWindow::on_wordSelected(const QString &word)
    {
        ui->contentStack->setCurrentWidget(wordPage);
        switchButtonCheckedState(ui->wordButton);
        wordPage->displayWord(word);
    }

    void MainWindow::onSearchWordAndSwitchPage(const QString &word)
    {
        ui->contentStack->setCurrentWidget(wordPage);
        switchButtonCheckedState(ui->wordButton);
        wordPage->displayWord(word);
    }


    void MainWindow::on_wordButton_clicked()
    {
        ui->contentStack->setCurrentWidget(wordPage);
        switchButtonCheckedState(ui->wordButton);
    }

    void MainWindow::on_studyButton_clicked()
    {
        ui->contentStack->setCurrentWidget(studyPage);
        switchButtonCheckedState(ui->studyButton);
    }

    void MainWindow::on_dataButton_clicked()
    {
        ui->contentStack->setCurrentWidget(dataPage);
        switchButtonCheckedState(ui->dataButton);
        dataPage->refreshPageData();
    }

    void MainWindow::on_communityButton_clicked()
    {
        ui->contentStack->setCurrentWidget(communityPage);
        switchButtonCheckedState(ui->communityButton);
    }

    void MainWindow::on_userButton_clicked()
    {
        ui->contentStack->setCurrentWidget(userPage);
        switchButtonCheckedState(ui->userButton);
    }

    void MainWindow::switchButtonCheckedState(QPushButton* button)
    {
        ui->wordButton->setChecked(false);
        ui->studyButton->setChecked(false);
        ui->dataButton->setChecked(false);
        ui->communityButton->setChecked(false);
        ui->userButton->setChecked(false);
        button->setChecked(true);
    }
}
